<?php

class User_m extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->library('email');
    }

    public function register($password, $email, $additional_data = false, $active=0) {
        $config['charset'] = 'utf-8';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';
        $this->email->initialize($config);

        $group_name = 'user';
        $group_id = $this->db->select('id')
                        ->where('name', $group_name)
                        ->get('users_groups')
                        ->row()->id;

        // IP Address
        $ip_address = $this->input->ip_address();
        $password = md5($password);

        // Users table.
        $data = array(
            'password' => $password,
            'email' => $email,
            'group_id' => $group_id,
            'ip_address' => $ip_address,
            'created_on' => now(),
            'last_login' => now(),
            'active' => $active
        );
        if ($this->db->insert('users', $data) > 0) {
            // Profile table.
            $id = $this->db->insert_id();
            $data = array('user_id' => $id);
            $columns = array('display_name', 'first_name', 'last_name', 'dob', 'gender', 'phone', 'mobile', 'gravatar');
            if (!empty($columns)) {
                foreach ($columns as $input) {
                    if (is_array($additional_data) && isset($additional_data[$input])) {
                        $data[$input] = $additional_data[$input];
                    } elseif ($this->input->post($input)) {
                        $data[$input] = $this->input->post($input);
                    }
                }
            }
            /* Customer address data */
            $address_data = array(
                'user_id' => $id,
                'date_add' => time(),
                'date_upd' => time()
            );
            $address_columns = array('first_name', 'last_name', 'company', 'address', 'phone');
            foreach ($address_columns as $input) {
                if (is_array($additional_data) && isset($additional_data[$input])) {
                    $address_data[$input] = $additional_data[$input];
                } elseif ($this->input->post($input)) {
                    $address_data[$input] = $this->input->post($input);
                }
            }
            $this->db->insert('address', $address_data);
            //send mail
            if ($active == 0) {
                $From = 'dungkirimi@gmail.com';
                $this->email->from($From, 'Bellizeno');
                $this->email->to($email);
                $this->email->subject(lang('register'));
                $message = 'Hello ' . $this->input->post('display_name') . "." . lang('note_reg') . '<a href="' . site_url('user/active_user' . '/' . $id . '">' . lang('confirm') . '</a>');
                $this->email->message($message);
                $this->email->send();
            }
        }
        return $this->db->insert('users_profiles', $data);
    }

    public function active_check($email = '') {
        if (empty($email)) {
            return FALSE;
        }
        return $this->db->where('email', $email)
                ->where('active', 0)
                ->count_all_results('users') > 0;
    }

    public function email_check($email = '') {
        if (empty($email)) {
            return FALSE;
        }
        return $this->db->where('email', $email)
                ->count_all_results('users') > 0;
    }

    public function active_user($user_id) {
        return $this->db->update('users', array('active' => 1), array('id' => $user_id));
    }

    public function get_account_infor_by_email($email) {
        $this->db->select('users.id,users_profiles.display_name,users.email');
        $this->db->from('users,users_profiles');
        $this->db->where('users.id = users_profiles.user_id');
        $this->db->where('users.email', $email);
        return $this->db->get();
    }

    function update_temp_pass($email, $data) {
        return $this->db->update('users', $data, array('email' => $email));
    }

    function get_user($email, $password) {
        $this->db->select('users.id,users.email,users.password,users.group_id,users_profiles.display_name');
        $this->db->from('users');
        $this->db->join('users_profiles', 'users_profiles.user_id = users.id');
        $this->db->where(array('users.email' => $email, 'users.password' => $password));
        $query = $this->db->get();
        return $query;
    }

    function change_login_time($data, $user_id) {
        return $this->db->update('users', $data, array('id' => $user_id));
    }
    function get_address_by_id($id_address){
        return $this->db->get_where('address',array('id_address'=>$id_address))->row();
    }
    function edit_address($posted_data,$id_address){
        $address_data = array('date_upd'=>time());
        $address_cols = array('first_name','last_name','company','address','phone','phone_mobile');
        foreach($address_cols as $col){
            if(array_key_exists($col,$posted_data)){
                $address_data[$col] = $posted_data[$col];    
            }
        }
        return $this->db->update('address',$address_data,array('id_address'=>$id_address));
    }
    function edit_address_order($address_data,$id_address){
        return $this->db->update('address',$address_data,array('id_address'=>$id_address));
    }
    function delete_address($id_address){
        return $this->db->delete('address',array('id_address'=>$id_address));
    }
    function add_address($posted_data){
        $address_data = array('user_id'=>$this->session->userdata('client_id'),'date_add'=>time());
        $address_cols = array('first_name','last_name','company','address','phone','phone_mobile');
        foreach($address_cols as $col){
            if(array_key_exists($col,$posted_data)){
                $address_data[$col] = $posted_data[$col];    
            }
        }
        return $this->db->insert('address',$address_data);
    }
}

?>
